<?php
error_reporting(0);
 function cut($file, $from, $end)
    {
        $message = explode($from, $file);
        $message = explode($end, $message[1]);
        return $message[0];
    }
    function trimall($str)
    {
        $qian = array("　", "                                                                                ", "                                ", "\t", "\n", "\r");
        $hou = array("", "", "", "", "");
        return str_replace($qian, $hou, $str);
    }
    function domain($url)
    {
        preg_match("/^(\\w+:\\/\\/)?([^\\/]+)/i", $url, $matches);
        $host = $matches[2];
        preg_match("/[^.\\/]+\\.[^.\\/]+\$/", $host, $matches);
        $urlext = $matches[0];
        return $urlext;
    }
$action = $_GET['action'];
$url = $_GET['url'];
if(!$url)
{
	exit(json_encode(array(msg=>0,ext=>'缺少必备参数url')));
}
if (!$action) {
    exit(json_encode(array(msg=>0,ext=>'缺少必备参数action')));
} elseif ($action == 1) {
	$contents = file_get_contents($url);
	$site = cut($contents, '<span class="txt">站点排序：</span>', '<div class="p-body-wrap">');
	if(!$site)
	{
		exit(json_encode(array(msg=>0,ext=>'错误的类型,电影为1，电视剧为2')));
	}
	$arr = explode('js-site', $site);
	foreach($arr as $row)
	{
		
		$play_url = str_replace('">', '', cut($row, 'href="', '"'));
            $play_num = 1;
            if ($play_url == '' || domain($play_url) == 'fun.tv') {
            } else {
			 $site_type = cut($row,'ea-site-' , '"' );
              $json_list[] = array(type=>$site_type,data=>array(num=>$play_num,url=>$play_url));
				
            }
	}
	$json_list[] = '"type":"1"';
	echo json_encode($json_list);
} elseif ($action == 2) {
    $contents = file_get_contents($url);
    $en_url = cut($contents, "id : '", "'");
    $site = cut($contents, '<span class="txt">站点排序:</span>', '</ul>');
    $arr = explode('<a data', $site);
	if(!$site)
	{
		exit(json_encode(array(msg=>0,ext=>'错误的类型,电影为1，电视剧为2')));
	}
	$play_json = '[';
    foreach ($arr as $key_num=>$row) {
        $site_type = cut($row, 'site="', '"');
        if (empty($site_type)) {
        } else {
            $json_url = "http://www.360kan.com/cover/switchsite?site={$site_type}&id={$en_url}&category=2";
            $url_contents = json_decode(file_get_contents($json_url))->data;
         $play_json .= '{"type":'.'"'.$site_type.'"'.",".'"data":[';
        }
        if (strpos($url_contents, '<div class="num-tab-main g-clear js-tab" style="display:block;">')) {
            $url_contents = trimall(cut($url_contents, '<div class="num-tab-main g-clear js-tab" style="display:block;">', '<a target="_self" class="all js-show-init" href="#" title="收起">收起</a>'));
        } else {
            $url_contents = trimall(cut($url_contents, '<div class="num-tab-main g-clear js-tab">', '</div>'));
        }
        $url_contents_arr = explode('</a>', $url_contents);
        foreach ($url_contents_arr as $key) {
            $play_url = str_replace('">', '', cut($key, 'href="', 'html'));
            $play_num = cut($key, 'data-num="', '"');
            if ($play_url == '' || domain($play_url) == 'fun.tv') {
            } else {
                //echo '"data"[{'.$play_num . '$' . $play_url . 'html$play#';
				$play_json .= json_encode(array(num=>$play_num,url=>$play_url.'html')).',';

				
            }
        }
        if (empty($site_type)) {
        } else {
          $i++;
			$play_json .= ']},';
        }
		
    }
	
	$play_json .= ']';
	//echo json_encode($json_list);
	$play_json= str_replace('},]},]','}]}]',$play_json);
	$play_json= str_replace('},]}','}]}',$play_json);
	echo $play_json;
	
}